import { apps } from "./app.js";
// app status
export const NOT_LOADED = "NOT_LOADED"
export const LOADING_SOURCE_CODE = "LOADING_SOURCE_CODE"
export const LOAD_ERROR = "LOAD_ERROR"

// 启动过程中
export const NOT_BOOTSTRAPED = "NOT_BOOTSTRAPED"
export const BOOTSTRAPING = "BOOTSTRAPING"
export const NOT_MOUNTED = "NOT_MOUNTED"


// 挂载流程
export const MOUNTING = "MOUNTING"
export const MOUNTED = "MOUNTED"

// 卸载流程
export const UNMOUNTING = "UNMOUNTING"

export const isActive = (app) => {
    return app.status === MOUNTED
}

export const shouldBeActive = (app) => {
    return app.activeWhen(window.location)
}

export function getAppChanges() {
    const appsToLoad = []
    const appsToMount = []
    const appsToUnMount = []

    apps.forEach((app)=> {
        let appShouldBeActive = shouldBeActive(app)
        switch (app.status) {
            case NOT_LOADED:
            case LOADING_SOURCE_CODE:
                // 应用被加载
                if(appShouldBeActive){
                    appsToLoad.push(app)
                }
                break;
            // 应用被挂载
            case NOT_BOOTSTRAPED:
            case BOOTSTRAPING:
            case NOT_MOUNTED:
                if(appShouldBeActive){
                    appsToMount.push(app)
                }
                break;
            // 应用被卸载
            case MOUNTED:
                if(!appShouldBeActive){
                    appsToUnMount.push(app)
                }
            default:
                break;
        }
    })

    return {
        appsToLoad,
        appsToMount,
        appsToUnMount
    }
}